package com.ruoyi.huanghefd.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.huanghefd.domain.AttachsheetOs;
import com.ruoyi.huanghefd.domain.AttachsheetOs1;
import com.ruoyi.huanghefd.domain.Makesheet;
import com.ruoyi.huanghefd.domain.MakesheetDetails;
import com.ruoyi.huanghefd.mapper.AttachsheetOs1Mapper;
import com.ruoyi.huanghefd.service.AttachsheetOsService;
import com.ruoyi.huanghefd.mapper.AttachsheetOsMapper;
import com.ruoyi.huanghefd.utils.PageBean;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 *
 */
@Service
public class AttachsheetOsServiceImpl extends ServiceImpl<AttachsheetOsMapper, AttachsheetOs>
        implements AttachsheetOsService {

    private static final Logger log = LoggerFactory.getLogger(AttachsheetOsServiceImpl.class);

    @Resource
    AttachsheetOsMapper attachsheetOsMapper;
    @Resource
    AttachsheetOs1Mapper attachsheetOs1Mapper;

    @Override
    public AjaxResult addAttachsheetOs(AttachsheetOs attachsheetOs) {
        int result = 0;
        try {
            //作业安全风险控制卡 主键
            String strAsId = UUID.randomUUID().toString().replace("-", "");
            //作业安全风险控制卡 票号
            String strAsCode = this.getGenerateOrder(attachsheetOs.getAsType());
            log.info("生成作业安全风险控制卡票号：" + strAsId);
            attachsheetOs.setAsCode(strAsCode);
            attachsheetOs.setAsId(strAsId);

            //插入 作业安全风险控制卡 主表
            log.info("新增作业安全风险控制卡-主表开始>>" + attachsheetOs);
            result = attachsheetOsMapper.insertAll(attachsheetOs);
            log.info("新增作业安全风险控制卡-主表结束<<" + result);

            //插入 作业安全风险控制卡 明细表 过程风险预控
            if (!CollectionUtils.isEmpty(attachsheetOs.getOs1())) {

                List<AttachsheetOs1> listmsdtl = new ArrayList<>();

                for (AttachsheetOs1 msdtls : attachsheetOs.getOs1()
                ) {
                    String uuid = UUID.randomUUID().toString().replace("-", "");
                    log.info("uuid=" + uuid);
                    AttachsheetOs1 msdtl = new AttachsheetOs1();
                    msdtl.setAsId(uuid);
                    msdtl.setAsCode(strAsCode);
                    msdtl.setAsOrder(msdtls.getAsOrder());
                    msdtl.setProcess(msdtls.getProcess());
                    msdtl.setSaftyDesc(msdtls.getSaftyDesc());
                    msdtl.setSaftyLevel(msdtls.getSaftyLevel());
                    msdtl.setDealMeasure(msdtls.getDealMeasure());
                    msdtl.setAsResult(msdtls.getAsResult());

                    log.info("msdtl=" + msdtl);
                    listmsdtl.add(msdtl);
                }
                log.info("新增作业安全风险控制卡-明细表开始>>" + listmsdtl);
                result = attachsheetOs1Mapper.insertBatch(listmsdtl);
                log.info("新增作业安全风险控制卡-明细表结束<<" + result);
            }
            return AjaxResult.success(strAsCode);
        } catch (Exception e) {
            return AjaxResult.error("作业安全风险控制卡新增失败", e);
        }
    }

    @Override
    public AjaxResult delAttachsheetOs(String strAsCode) {
        int result = 0;
        try {
            log.info("删除作业安全风险控制卡-主表开始>>");
            result = attachsheetOsMapper.delByAsCode(strAsCode);
            log.info("删除作业安全风险控制卡-主表结束<<" + result);

            log.info("删除作业安全风险控制卡-从表开始>>");
            result = attachsheetOs1Mapper.delByAsCode(strAsCode);
            log.info("删除作业安全风险控制卡-从表结束<<" + result);

            return AjaxResult.success(strAsCode);
        } catch (Exception e) {
            return AjaxResult.error("作业安全风险控制卡删除失败", e);
        }
    }

    @Override
    public AjaxResult uptAttachsheetOs(AttachsheetOs attachsheetOs) {
        int result = 0;
        try {
            log.info("修改作业安全风险控制卡-主表开始>>");
            result = attachsheetOsMapper.updateByAsId(attachsheetOs);
            log.info("修改作业安全风险控制卡-主表结束<<" + result);

            if (!CollectionUtils.isEmpty(attachsheetOs.getOs1())) {
                log.info("修改作业安全风险控制卡-从表开始>>");
                //result = attachsheetOs1Mapper.updateBatchByAsId(attachsheetOs.getOs1());
                //最好的方式还是先删后插
                attachsheetOs1Mapper.delByAsCode(attachsheetOs.getAsCode());
                attachsheetOs1Mapper.insertBatch(attachsheetOs.getOs1());

                log.info("修改作业安全风险控制卡-从表结束<<" + result);
            }
            return AjaxResult.success(attachsheetOs.getAsCode());
        } catch (Exception e) {
            return AjaxResult.error("作业安全风险控制卡修改失败", e);
        }
    }

    @Override
    public AjaxResult queryAttachsheetOss(String strAsCode, String strAsState, String strUserId, String strMailType) {
        try {
            List<AttachsheetOs> attachsheetOsList = attachsheetOsMapper.queryAttachsheetOsList(strAsCode, strAsState, strUserId, strMailType);
            if (CollectionUtils.isEmpty(attachsheetOsList)) {
                return AjaxResult.success("作业安全风险控制卡查询无数据", null);
            }
            return AjaxResult.success("作业安全风险控制卡查询成功", attachsheetOsList);
        } catch (Exception e) {
            return AjaxResult.warn("作业安全风险控制卡查询失败", e);
        }
    }

    @Override
    public PageBean queryAttachsheetOssByPage(String strAsCode, String strAsState, String strUserId, String strMailType, int pageNum, int pageSize) {
        PageBean pageBean = new PageBean();
        if (pageNum > 0 && pageSize > 0) {
            int totalRecord = attachsheetOsMapper.getAttachsheetOsNum(strAsCode, strAsState, strUserId, strMailType);
            pageBean = new PageBean(pageNum, pageSize, totalRecord);

            //从该行开始拿数据
            int startIndex = (pageNum - 1) * pageSize;
            pageBean.setStartIndex(startIndex);
            pageBean.setCode(200);

            //数据集合
            List<AttachsheetOs> attachsheetOsList = attachsheetOsMapper.queryAttachsheetOsListByPage(strAsCode, strAsState, strUserId, strMailType, startIndex, pageSize);
            pageBean.setList(attachsheetOsList);
            pageBean.setSuccessMsg("作业安全风险控制卡查询成功！");
        } else {
            pageBean.setCode(500);
            pageBean.setFailMsg("作业安全风险控制卡查询失败！");
        }
        return pageBean;
    }

    /**
     * @param Type
     * @return java.lang.String
     * @Author gnplum
     * @Description //操作票票号
     * @Date 14:56 2023/11/12
     **/
    private String getGenerateOrder(String Type) {

        log.info("获取票编号=" + Type);
        String strWsCode = "";
        int order = 0;
        try {

            order = attachsheetOsMapper.getAsCodeNum(Type);
            log.info("order=" + order);
            //DQ1202311110001
            strWsCode = Type + new SimpleDateFormat("yyyyMMdd").format(new Date()) + String.format("%03d", order);

            log.info("strWsCode=" + strWsCode);

        } catch (Exception e) {
            strWsCode = "";
            //e.printStackTrace();
            //throw  e;
            //log.error(e.getCause()+e.getMessage());
        }
        return strWsCode;
    }
}




